class Actions {
  // 默认空实现
  defaultOnGlobalStateChange = () => {};
  defaultSetGlobalState = () => false;
  defaultGetGlobalState = () => ({});
  defaultOffGlobalStateChange = () => {};

  actions = {
    // 注册观察者
    onGlobalStateChange: null,
    // 修改全局状态
    setGlobalState: null,
    // 获取全局状态
    getGlobalState: null,
    // 取消监听器
    offGlobalStateChange: null,
  };

  setActions(actions) {
    
    this.actions = actions;
    
  }
  onGlobalStateChange() {
    if (typeof this.actions.onGlobalStateChange !== 'function') {
      console.error('onGlobalStateChange is not properly initialized');
      return this.defaultOnGlobalStateChange;
    }
    return this.actions.onGlobalStateChange(...arguments);
  }
  
  setGlobalState() {
    if (typeof this.actions.setGlobalState !== 'function') {
      console.error('setGlobalState is not properly initialized');
      return false;
    }
    return this.actions.setGlobalState(...arguments);
  }
  
  getGlobalState() {
    if (typeof this.actions.getGlobalState !== 'function') {
      console.error('getGlobalState is not properly initialized');
      return {};
    }
    return this.actions.getGlobalState(...arguments);
  }
  
  offGlobalStateChange() {
    if (typeof this.actions.offGlobalStateChange !== 'function') {
      console.error('offGlobalStateChange is not properly initialized');
      return;
    }
    return this.actions.offGlobalStateChange(...arguments);
  }
}

// 创建单例实例
const ActionsInstance = new Actions();

// 如果不是在 qiankun 环境中运行，设置默认实现
if (typeof window !== 'undefined' && !window.__POWERED_BY_QIANKUN__) {
  ActionsInstance.setActions({
    onGlobalStateChange: ActionsInstance.defaultOnGlobalStateChange,
    setGlobalState: ActionsInstance.defaultSetGlobalState,
    getGlobalState: ActionsInstance.defaultGetGlobalState,
    offGlobalStateChange: ActionsInstance.defaultOffGlobalStateChange
  });
}

export default ActionsInstance;
